package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author 86182
* @description 针对表【room_info(房间信息表)】的数据库操作Service实现
* @createDate 2024-09-20 18:04:34
*/
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
    implements RoomInfoService{
    @Autowired
    RoomInfoMapper roomInfoMapper;
    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    LabelInfoMapper labelInfoMapper;
    @Autowired
    FacilityInfoMapper facilityInfoMapper;
    @Autowired
    GraphInfoMapper graphInfoMapper;
    @Autowired
    PaymentTypeMapper paymentTypeMapper;
    @Autowired
    LeaseTermMapper leaseTermMapper;
    @Autowired
    AttrValueMapper attrValueMapper;


    @Autowired
    RoomAttrValueService roomAttrValueService;
    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    RoomFacilityService roomFacilityService;
    @Autowired
    RoomLabelService roomLabelService;
    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    RoomLeaseTermService roomLeaseTermService;



    @Override
    public IPage<RoomItemVo> pageItemInfo(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItemInfo(roomItemVoPage,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        // 查询房间的基本信息
        RoomInfo roomInfo = this.getById(id);

        // 根据公寓的id查询公寓的基本信息 -> 查询的是房间所属的公寓
        Long apartmentId = roomInfo.getApartmentId();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(apartmentId);

        // 根据房间的id查询图片信息
        List<GraphVo> graphVoList = graphInfoMapper.selectListByIdAndType(roomInfo.getId(), ItemType.ROOM);
        // 根据房间的id查询属性信息
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectListByRoomId(roomInfo.getId());
        // 根据房间的id查询配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(roomInfo.getId());
        // 根据房间的id查询标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(roomInfo.getId());
        // 根据房间的id查询支付类型
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(roomInfo.getId());
        // 根据房间的id查询租期
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(roomInfo.getId());


        // 把数据保存到RoomDetailVo
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        BeanUtils.copyProperties(roomInfo,roomDetailVo);

        return roomDetailVo;
    }

    @Override
    public void saveOrUpdateInfo(RoomSubmitVo roomSubmitVo) {
        // 房间的id
        Long id = roomSubmitVo.getId();

        // 添加和修改房间的基本信息 --> 如果是添加  自动返回最新的id
        this.saveOrUpdate(roomSubmitVo);



        if (id!= null){
            // 修改 --> 删除和房间建立关系的关系表数据
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,id));
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,id));
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId,id));
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,id));
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id).eq(GraphInfo::getItemType,ItemType.ROOM));
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,id));
        }

        // 添加新的关系
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfos = new ArrayList<>();
            for (GraphVo graphVo : graphVoList){
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setItemType(ItemType.ROOM);
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }

        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();

        if (!CollectionUtils.isEmpty(attrValueIds)){
            List<RoomAttrValue> roomAttrValues = new ArrayList<>();
            for (Long attrValueId : attrValueIds){
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().build();
                roomAttrValue.setRoomId(roomSubmitVo.getId());
                roomAttrValue.setAttrValueId(attrValueId);
                roomAttrValues.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValues);
        }

        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();

        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            List<RoomFacility> roomFacilities = new ArrayList<>();
            for (Long facilitieId : facilityInfoIds){
                RoomFacility roomFacility = RoomFacility.builder().build();
                roomFacility.setRoomId(roomSubmitVo.getId());
                roomFacility.setFacilityId(facilitieId);
                roomFacilities.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilities);
        }

        //4.保存新的labelInfoList
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)) {
            ArrayList<RoomLabel> roomLabelList = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }

        //5.保存新的paymentTypeList
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
            ArrayList<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }

        //6.保存新的leaseTermList
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)) {
            ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
                roomLeaseTerms.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }
    }

    @Override
    public void removeByIdInfo(Long id) {
        // 1. 删除房间信息
        this.removeById(id);

        //  删除和房间建立关系的关系表数据

        // 2. 删除图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id).eq(GraphInfo::getItemType,ItemType.ROOM);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);

        // 3. 删除房间属性信息
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

        // 4. 删除房间设施信息
        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(roomFacilityLambdaQueryWrapper);

        // 5. 删除房间标签信息
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,id);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);

        // 6. 删除房间付款方式信息
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

        // 7. 删除房间租期信息
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
    }
}




